package com.lovo.dao.impl;

import com.lovo.dao.BaseDAO;
import com.lovo.dao.UserDAO;
import com.lovo.pojo.UserDO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    /**
     * 根据用户名和密码查询用户对象
     * @param name
     * @param pwd
     * @return
     */

    public UserDO getByusernameAndPwd(String name, String pwd) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name=? and u_password=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, name);
            statement.setObject(2, pwd);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setName(rs.getString("u_name"));
                user.setPwd(rs.getString("u_password"));
                user.setMoney(rs.getDouble("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    @Override
    public UserDO getById(int id) {
            con = getConnection();
        statement = null;
            rs = null;
            UserDO user = null;
            String sql = "select * from t_user where u_id=?";
            try {
                statement = con.prepareStatement(sql);
                statement.setObject(1,id);
                rs = statement.executeQuery();
                if(rs.next()){
                    user = new UserDO();
                    user.setId(rs.getInt("u_id"));
                    user.setName(rs.getString("u_name"));
                    user.setPwd(rs.getString("u_password"));
                    user.setMoney(rs.getDouble("u_money"));
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                close(con,statement,rs);
            }
            return user;
    }

    public void update(UserDO user) {
        this.getConnection();
        String sql = "update t_user set u_password=?,u_money=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getPwd());
            statement.setObject(2, user.getMoney());
            statement.setObject(3, user.getId());
            // 3. 执行SQL语句
            int row = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
            close(con, statement);
        }
    }

    @Override
    public UserDO getByUserName(String name) {
        this.getConnection();
        statement = null;
         rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,name);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setName(rs.getString("u_name"));
                user.setPwd(rs.getString("u_password"));
                user.setMoney(rs.getDouble("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    public void setAutoCommit(boolean autoCommit) {
        try {
            con.setAutoCommit(autoCommit);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
